package gui;

import gui.houseeditor.HouseEditorPane;
import gui.houseeditor.HouseModel;
import gui.houseeditor.HouseModel.HouseObject;
import io.BaseHouseReader;
import io.HouseChars;

import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

import sde.actor.ControlMachine;
import sde.actor.Realtime;
import util.Casella;
import actor.sensori.Sensore;
import actor.sensori.SensorePorta;
import actor.sensori.SensoreVolume;
import attori.attuatori.AttuatoreLuce;
import attori.attuatori.AttuatoreLuceIF;
import attori.attuatori.AttuatorePorta;
import attori.attuatori.AttuatorePortaIF;
import attori.controller.ControllerLuce;
import attori.controller.ControllerLuceIF;
import attori.controller.ControllerPorta;
import attori.controller.ControllerPortaIF;
import attori.model.Stanza;

@SuppressWarnings("serial")
public class MainFrame extends JFrame {
	private JScrollPane centerPane;
	private HouseEditorPane housePane;
	private HouseModel model;

	public MainFrame() {
		super("Casa Intelligente");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		// initSidebar();
		// initToolbar();
		// initMenubar();
		initCenterPanel();
		this.addKeyListener(new KeyHandler());
		// initFindTextField();
		// initPopupMenu();
		// popolaTabella(db.getArticoli());
		// filterCode.setColumns(15);
		// filterCode.addMouseListener(new MouseAdapter() {
		//
		// @Override
		// public void mousePressed(MouseEvent e) {
		// SwingUtilities.invokeLater(new Runnable() {
		// @Override
		// public void run() {
		// filterCode.selectAll();
		// }
		// });
		// }
		// });
		// JPopupMenu menu = new JPopupMenu();
		// JMenuItem incolla = new JMenuItem("Incolla BarCode");
		// incolla.setAction(incollaAction);
		// menu.add(incolla);
		// filterCode.add(menu);
		// filterCode.setComponentPopupMenu(menu);
		// find.add(findLabel);
		// find.add(filterCode);
		// add(sidebar, BorderLayout.WEST);
		// add(toolBar, BorderLayout.NORTH);
		// add(table, BorderLayout.CENTER);
		// rimuovi.setEnabled(false);
		// setStartPosition(StartPosition.CenterInScreen);
		// add(find, BorderLayout.SOUTH);
		// pack();
		// filterCode.requestFocusInWindow();
		housePane.repaint();
		model = housePane.getHouseModel();
		setLocationByPlatform(true);
		pack();
	}

	private void initCenterPanel() {
		File f = new File("mappe/casaProva");
		try {
			readFromFile(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void readFromFile(File inFile) throws IOException {
		BaseHouseReader reader = new BaseHouseReader(new FileReader(inFile));
		housePane = new HouseEditorPane(reader.getWidth(), reader.getHeight());
		housePane.setCellSize(22);
		for (int i = 0; i < reader.getHeight(); i++)
			for (int j = 0; j < reader.getWidth(); j++) {
				switch (reader.get(i, j)) {
				case HouseChars.BLANK:
					housePane.getHouseModel().setCell(
							HouseModel.HouseObject.BLANK, i, j);
					break;

				case HouseChars.LAMPADINA:
					housePane.getHouseModel().setCell(
							HouseModel.HouseObject.LAMPADINA_SPENTA, i, j);
					break;

				case HouseChars.PORTA:
					housePane.getHouseModel().setCell(
							HouseModel.HouseObject.PORTA, i, j);
					break;

				case HouseChars.WALL:
					housePane.getHouseModel().setCell(
							HouseModel.HouseObject.WALL, i, j);
					break;

				case HouseChars.PERSONA:
					housePane.getHouseModel().setCell(
							HouseModel.HouseObject.PERSONA, i, j);
					break;
				}
			}
		centerPane = new JScrollPane(housePane);
		MainFrame.this.add(centerPane, BorderLayout.CENTER);
		MainFrame.this.validate();

	}

	private class KeyHandler extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			int x = getPersonaX();
			int y = getPersonaY();
			int keyCode = e.getKeyCode();
			if (keyCode == KeyEvent.VK_DOWN)
				if (model.getCell(x + 1, y) == HouseObject.BLANK) {
					model.setCell(HouseObject.BLANK, x, y);
					model.setCell(HouseObject.PERSONA, x + 1, y);
				}
			if (keyCode == KeyEvent.VK_UP)
				if (model.getCell(x - 1, y) == HouseObject.BLANK) {
					model.setCell(HouseObject.BLANK, x, y);
					model.setCell(HouseObject.PERSONA, x - 1, y);
				}
			if (keyCode == KeyEvent.VK_LEFT)
				if (model.getCell(x, y - 1) == HouseObject.BLANK) {
					model.setCell(HouseObject.BLANK, x, y);
					model.setCell(HouseObject.PERSONA, x, y - 1);
				}
			if (keyCode == KeyEvent.VK_RIGHT)
				if (model.getCell(x, y + 1) == HouseObject.BLANK) {
					model.setCell(HouseObject.BLANK, x, y);
					model.setCell(HouseObject.PERSONA, x, y + 1);
				}
		}
	}

	public int getPersonaY() {
		for (int i = 0; i < model.getHeight(); i++) {
			for (int j = 0; j < model.getWidth(); j++) {
				if (model.getCell(i, j) == HouseObject.PERSONA) {
					return j;
				}
			}
		}
		return -1;
	}

	public int getPersonaX() {
		for (int i = 0; i < model.getHeight(); i++) {
			for (int j = 0; j < model.getWidth(); j++) {
				if (model.getCell(i, j) == HouseObject.PERSONA) {
					return i;
				}
			}
		}
		return -1;
	}

	public HouseModel getModel() {
		return model;
	}

	public static void main(String[] args) {
		ControlMachine cm = new Realtime();
		MainFrame f = new MainFrame();
		f.setVisible(true);
		HouseModel model = f.getModel();
		// Porta
		Casella[] loco = new Casella[] { new Casella(20, 12), new Casella(21, 12) };
		AttuatorePortaIF ap = new AttuatorePorta();
		ap.send(new AttuatorePortaIF.Init(model, loco));
		ControllerPortaIF ctl = new ControllerPorta();
		ctl.send(new ControllerPortaIF.Init(ap, 1000));
		Casella[][] porta = new Casella[][] {
				{ new Casella(20, 11), new Casella(20, 12), new Casella(20, 13) },
				{ new Casella(21, 11), new Casella(21, 12), new Casella(21, 13) } };
		Sensore s = new SensorePorta(model, porta, ctl, 500);
		// Luce
		AttuatoreLuceIF al = new AttuatoreLuce();
		Stanza prova = new Stanza(25,1,1,model,15,26,2,11);
		al.send(new AttuatoreLuceIF.Init(prova));
		ControllerLuceIF ctp = new ControllerLuce();
		ctp.send(new ControllerLuceIF.Init(al, 1));
		Sensore sl = new SensoreVolume(prova, ctp, 100);
		cm.controller();
	}
}
